﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Adjust_NegativeIQ : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private List<int> change_row;


        public frm_Adjust_NegativeIQ()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);

            this.change_row = new List<int>();
            
            LoadData();
            AdjustNegativeIQ.CellValueChanged += ChangeQuantity_CellValueChanged;
        }

        void ChangeQuantity_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            AdjustNegativeIQ.PostEditor();
            AdjustNegativeIQ.UpdateCurrentRow();
            change_row.Add(AdjustNegativeIQ.FocusedRowHandle);
        }

        ///////////////////////////////////
        /// Load Data From Local Database
        ///////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;

            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT * FROM [241_ConsumptionAdjust] ORDER BY AdjustedMass";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridAdjustNegativeIQ.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Adjust");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("RZCode", typeof(string));
                newTable.Columns.Add("IQID", typeof(string));
                newTable.Columns.Add("MassQuantity", typeof(float));
                newTable.Columns.Add("NetQuantity", typeof(float));
                newTable.Columns.Add("AdjustedMass", typeof(float));
                newTable.Columns.Add("RobotQuantity", typeof(float));
                newTable.Columns.Add("IQUnit", typeof(string));
                gridAdjustNegativeIQ.DataSource = newTable;

                ret = false;
            }
            AdjustNegativeIQ.Columns[0].Caption = "RZ Code";
            AdjustNegativeIQ.Columns[1].Caption = "IQ ID";
            AdjustNegativeIQ.Columns[2].Caption = "Mass Quantity";
            AdjustNegativeIQ.Columns[3].Caption = "Net Quantity";
            AdjustNegativeIQ.Columns[4].Caption = "Adjusted Mass";
            AdjustNegativeIQ.Columns[5].Caption = "Robot Quantity";
            AdjustNegativeIQ.Columns[6].Caption = "IQUnit";            
            AdjustNegativeIQ.BestFitColumns();
            AdjustNegativeIQ.Columns[0].OptionsColumn.AllowEdit = false;
            AdjustNegativeIQ.Columns[1].OptionsColumn.AllowEdit = false;
            AdjustNegativeIQ.Columns[2].OptionsColumn.AllowEdit = false;
            AdjustNegativeIQ.Columns[3].OptionsColumn.AllowEdit = false;
            AdjustNegativeIQ.Columns[5].OptionsColumn.AllowEdit = false;
            AdjustNegativeIQ.Columns[6].OptionsColumn.AllowEdit = false;

            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            UpdateChange();
        }

        private void UpdateChange()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                for (int i = 0; i < AdjustNegativeIQ.RowCount; i++ )
                {
                    DataRow row = AdjustNegativeIQ.GetDataRow(i);
                    float adjust_mass = 0;
                    if (float.TryParse(row["AdjustedMass"].ToString(), out adjust_mass))
                    {
                        cmd_sql.CommandText = "UPDATE [241_ConsumptionAdjust] SET AdjustedMass=" + adjust_mass
                            + " WHERE RZCode='" + row[0].ToString() + "' AND IQID = '" + row[1].ToString() + "'";
                        cmd_sql.ExecuteNonQuery();
                    }
                }
                cmd_sql.Dispose();
                myConnection.Close();

                MessageBox.Show("Update successfully!");
                LoadData();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
                //MessageBox.Show("Cannot update database");
            }
        }       
    }
}
